package com.ruoyi.safe.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NameUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.safe.domain.CommonCooperator;
import com.ruoyi.safe.domain.CommonCooperatorTeam;
import com.ruoyi.safe.domain.CommonProject;
import com.ruoyi.safe.domain.vo.CommonCooperatorTeamVO;
import com.ruoyi.safe.mapper.CommonCooperatorMapper;
import com.ruoyi.safe.mapper.CommonCooperatorTeamMapper;
import com.ruoyi.safe.mapper.CommonProjectMapper;
import com.ruoyi.safe.service.ICommonCooperatorTeamService;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 企业班组Service业务层处理
 *
 * @author buxi
 * @date 2023-12-20
 */

@Service
@RequiredArgsConstructor
public class CommonCooperatorTeamServiceImpl implements ICommonCooperatorTeamService {

    private final CommonCooperatorTeamMapper commonCooperatorTeamMapper;

    private final CommonCooperatorMapper commonCooperatorMapper;

    private final CommonProjectMapper commonProjectMapper;

    private final SysUserMapper sysUserMapper;

    /**
     * 查询企业班组
     *
     * @param id 企业班组主键
     * @return 企业班组
     */
    @Override
    public CommonCooperatorTeam selectCommonCooperatorTeamById(Long id) {
        return commonCooperatorTeamMapper.selectCommonCooperatorTeamById(id);
    }

    /**
     * 查询企业班组列表
     *
     * @param commonCooperatorTeam 企业班组
     * @return 企业班组
     */
    @Override
    public List<CommonCooperatorTeamVO> selectCommonCooperatorTeamList(CommonCooperatorTeam commonCooperatorTeam) {
        List<CommonCooperatorTeam> commonCooperatorTeams = commonCooperatorTeamMapper.selectCommonCooperatorTeamList(commonCooperatorTeam);
        //查出所有的企业名称
        CommonProject commonProject = commonProjectMapper.selectCommonProjectById(commonCooperatorTeam.getProjectId());
        List<CommonCooperator> commonCooperators = commonCooperatorMapper.selectSubPackageList();
        List<CommonCooperatorTeamVO> commonCooperatorTeamVOList = commonCooperatorTeams.stream().map(commonCooperatorTeam1 -> {
            CommonCooperatorTeamVO commonCooperatorTeamVO = new CommonCooperatorTeamVO();
            BeanUtil.copyProperties(commonCooperatorTeam1, commonCooperatorTeamVO);
            //设置企业名称
            commonCooperators.stream()
                    .filter(commonCooperator -> commonCooperator.getId().equals(commonCooperatorTeam1.getCooperatorId()))
                    .findFirst()
                    .ifPresent(commonCooperator -> commonCooperatorTeamVO.setCooperatorName(commonCooperator.getEnterpriseName()));
            commonCooperatorTeamVO.setProjectName(commonProject.getName());
            return commonCooperatorTeamVO;
        }).collect(Collectors.toList());
        //设置创建人名称
        NameUtils.handleUserNameToNickName(commonCooperatorTeamVOList,sysUserMapper.selectUserListAll(),"createBy");
        return commonCooperatorTeamVOList;
    }

    /**
     * 新增企业班组
     *
     * @param commonCooperatorTeam 企业班组
     * @return 结果
     */
    @Override
    public int insertCommonCooperatorTeam(CommonCooperatorTeam commonCooperatorTeam) {
        commonCooperatorTeam.setCreateBy(SecurityUtils.getUsername());
        commonCooperatorTeam.setCreateTime(DateUtils.getNowDate());
        return commonCooperatorTeamMapper.insertCommonCooperatorTeam(commonCooperatorTeam);
    }

    /**
     * 修改企业班组
     *
     * @param commonCooperatorTeam 企业班组
     * @return 结果
     */
    @Override
    public int updateCommonCooperatorTeam(CommonCooperatorTeam commonCooperatorTeam) {
        commonCooperatorTeam.setUpdateTime(DateUtils.getNowDate());
        return commonCooperatorTeamMapper.updateCommonCooperatorTeam(commonCooperatorTeam);
    }

    /**
     * 批量删除企业班组
     *
     * @param ids 需要删除的企业班组主键
     * @return 结果
     */
    @Override
    public int deleteCommonCooperatorTeamByIds(Long[] ids) {
        return commonCooperatorTeamMapper.deleteCommonCooperatorTeamByIds(ids);
    }

    /**
     * 删除企业班组信息
     *
     * @param id 企业班组主键
     * @return 结果
     */
    @Override
    public int deleteCommonCooperatorTeamById(Long id) {
        return commonCooperatorTeamMapper.deleteCommonCooperatorTeamById(id);
    }

    @Override
    public List<CommonCooperatorTeam> selectCommonCooperatorTeamListByProjectId(Long projectId) {
        return commonCooperatorTeamMapper.selectCommonCooperatorTeamList(CommonCooperatorTeam.builder().projectId(projectId).build());
    }
}
